<?php

class Model_fitur extends CI_Model {

	//-------TFIDF METHOD START-------
	public function tfidf_token_kunci($kalimat){
		$kunci['token'] = "";
		$loop_1 		= 0;
		foreach($kalimat['check_kunci'] as $kunci_index){
			$kunci['no'][$kunci_index] 		= $kunci_index;
			$kunci['asli'][$kunci_index] 	= trim($kalimat['kunci'][$kunci_index]);
			$kunci['kunci'][$kunci_index] 	= $this->simbol(trim($kunci['asli'][$kunci_index]));
			$kunci['kunci'][$kunci_index] 	= $this->filtering(trim($kunci['kunci'][$kunci_index]));
			$kunci['token'] 				= trim($kunci['token'])." ".trim($kunci['kunci'][$kunci_index]);
			
			$kunci['hasil'][$kunci_index] 	= trim($kunci['kunci'][$kunci_index]);
			$kunci['explode'][$kunci_index] = explode(" ", $kunci['hasil'][$kunci_index]);
			$kunci['jumlah'][$kunci_index] 	= count($kunci['explode'][$kunci_index]);
			
			if($loop_1 == 0){
				$kunci['ngram'] = $kunci['jumlah'][$kunci_index];
			}	
			else{
				if($kunci['ngram'] > $kunci['jumlah'][$kunci_index]){
					$kunci['ngram'] = $kunci['jumlah'][$kunci_index];
				}
				else{
					$kunci['ngram'] = $kunci['ngram'];
				}
			}
			$loop_1++;
		}
		return $kunci;
	}
	
	public function tfidf_token_jawaban($kalimat, $kunci_token){
		$jawaban['token'] 	= "";
		$loop_2 			= 0;
		foreach($kalimat['check_jawaban'] as $jawaban_index){
			$jawaban['no'][$jawaban_index] 					= $jawaban_index;
			$jawaban['asli'][$jawaban_index] 				= trim($kalimat['jawaban_siswa'][$jawaban_index]);
			$jawaban['jawaban'][$jawaban_index] 			= $this->simbol(trim($jawaban['asli'][$jawaban_index]));
			$jawaban['jawaban'][$jawaban_index] 			= $this->filtering(trim($jawaban['jawaban'][$jawaban_index]));
			$jawaban['token'][$jawaban_index] 				= trim($jawaban['jawaban'][$jawaban_index])." ".trim($kunci_token);
			
			$jawaban['token'][$jawaban_index] 				= $this->tokenizing(trim($jawaban['token'][$jawaban_index]));
			$jawaban['token']['explode'][$jawaban_index] 	= explode(" ",trim($jawaban['token'][$jawaban_index]));
			$jawaban['token']['jumlah'][$jawaban_index] 	= count($jawaban['token']['explode'][$jawaban_index]);
			
			$jawaban['hasil'][$jawaban_index] 				= trim($jawaban['jawaban'][$jawaban_index]);
			$jawaban['explode'][$jawaban_index] 			= explode(" ", $jawaban['hasil'][$jawaban_index]);
			$jawaban['jumlah'][$jawaban_index] 			= count($jawaban['explode'][$jawaban_index]);
			
			if($loop_2 == 0){
				$jawaban['ngram'] = $jawaban['jumlah'][$jawaban_index];
			}	
			else{
				if($jawaban['ngram'] > $jawaban['jumlah'][$jawaban_index]){
					$jawaban['ngram'] = $jawaban['jumlah'][$jawaban_index];
				}
				else{
					$jawaban['ngram'] = $jawaban['ngram'];
				}
			}
			$loop_2++;
		}
		return $jawaban;
	}
	
	public function matching_tfidf($kalimat, $ngram){
		foreach($kalimat['jawaban']['no'] as $no_jawaban){
			$S1['explode'] 			= $kalimat['jawaban']['token']['explode'][$no_jawaban];
			$S1['jumlah'] 			= $kalimat['jawaban']['token']['jumlah'][$no_jawaban];
			$S2_jawaban['explode'] 	= $kalimat['jawaban']['explode'][$no_jawaban];
			$S2_jawaban['jumlah'] 	= $kalimat['jawaban']['jumlah'][$no_jawaban];
			
			$hasil['no'][$no_jawaban] 			= $no_jawaban;
			$hasil['jawaban_asli'][$no_jawaban] = $S2_jawaban['explode'];
			$hasil['jawaban'][$no_jawaban] 		= $this->matching_kata($S1, $S2_jawaban, $ngram);
			
			foreach($kalimat['kunci']['no'] as $no_kunci){
				$S2_kunci['explode'] 	= $kalimat['kunci']['explode'][$no_kunci];
				$S2_kunci['jumlah'] 	= $kalimat['kunci']['jumlah'][$no_kunci];
				
				$hasil['no_kunci'][$no_kunci] 			= $no_kunci;
				$hasil['kunci'][$no_jawaban][$no_kunci] = $this->matching_kata($S1, $S2_kunci, $ngram);
			}
		}
		$hasil['jumlah_kunci'] = count($kalimat['kunci']['no'])+ 1;
		return $hasil;
	}
	
	public function tfidf_normal($hasil){
		foreach($hasil['no'] as $no_jawaban){
			foreach($hasil['no_kunci'] as $no_kunci){
				if($hasil['cosin'][$no_jawaban][$no_kunci]!=0){
					$nilai[$no_jawaban][$no_kunci] = ($hasil['cosin'][$no_jawaban][$no_kunci]/$hasil['cosin']['jawaban'][$no_jawaban]) * $hasil['skor'][$no_kunci];
				}
				else{
					$nilai[$no_jawaban][$no_kunci] = 0;
				}
			}
		}
		return $nilai;
	}
	
	public function nilai($hasil){
		foreach($hasil['no'] as $no_jawaban){
			$nilai[$no_jawaban] = 0;
			
			foreach($hasil['no_kunci'] as $no_kunci){
				if($nilai[$no_jawaban]>=$hasil['normal'][$no_jawaban][$no_kunci]){
					$nilai[$no_jawaban] = round($nilai[$no_jawaban],$hasil['error_digit']);
				}
				else{
					$nilai[$no_jawaban] = round($hasil['normal'][$no_jawaban][$no_kunci],$hasil['error_digit']);
				}
			}
		}
		return $nilai;
	}
	//-------TFIDF METHOD END-------
	
	
	//-------BLEU METHOD START-------
	public function bleu_token_kalimat($kalimat){
		foreach($kalimat['check_kunci'] as $kunci_index){
			$kalimat['kunci'][$kunci_index] = $this->simbol(trim($kalimat['kunci'][$kunci_index]));
			$kalimat['kunci'][$kunci_index] = $this->filtering(trim($kalimat['kunci'][$kunci_index]));
			$kalimat['kunci'][$kunci_index] = $this->tokenizing(trim($kalimat['kunci'][$kunci_index]));
			
			$kalimat['kunci']['explode'][$kunci_index] = explode(" ",trim($kalimat['kunci'][$kunci_index]));
			$kalimat['kunci']['jumlah'][$kunci_index]  = count($kalimat['kunci']['explode'][$kunci_index]);
			
			$kalimat['no_kunci'][$kunci_index]  = $kunci_index;
			$kalimat['skor'][$kunci_index]		= $kalimat['skor'][$kunci_index];
		}
		
		foreach($kalimat['check_jawaban'] as $jawaban_index){
			$kalimat['jawaban'][$jawaban_index] = $this->simbol(trim($kalimat['jawaban_siswa'][$jawaban_index]));
			$kalimat['jawaban'][$jawaban_index] = $this->filtering(trim($kalimat['jawaban'][$jawaban_index]));
			$kalimat['jawaban'][$jawaban_index] = $this->tokenizing(trim($kalimat['jawaban'][$jawaban_index]));
			
			$kalimat['jawaban']['explode'][$jawaban_index] = explode(" ",trim($kalimat['jawaban'][$jawaban_index]));
			$kalimat['jawaban']['jumlah'][$jawaban_index]  = count($kalimat['jawaban']['explode'][$jawaban_index]);
			
			$kalimat['no'][$jawaban_index] = $jawaban_index;
		}
		
		return $kalimat;	
	}
	
	public function matching_bleu($kalimat){
		foreach($kalimat['no_kunci'] as $no_kunci){
			$hasil['no_kunci'][$no_kunci] = $no_kunci;
			foreach($kalimat['no'] as $no_jawaban){
				$hasil['no'][$no_jawaban] 				= $no_jawaban;
				$hasil['log'][$no_kunci][$no_jawaban]   = 0;
				$hasil['e']								= $kalimat['e'];
				$hasil['error_digit']					= $kalimat['error_digit'];
				for($ngram=1; $ngram<=4; $ngram++){
					$hasil[$no_kunci]['kunci']['explode'] 	 = $kalimat['kunci']['explode'][$no_kunci];
					$hasil[$no_kunci]['kunci']['jumlah'] 	 = $kalimat['kunci']['jumlah'][$no_kunci];
					$hasil[$no_jawaban]['jawaban']['explode']= $kalimat['jawaban']['explode'][$no_jawaban];
					$hasil[$no_jawaban]['jawaban']['jumlah'] = $kalimat['jawaban']['jumlah'][$no_jawaban];
					
					$hasil['hasil_hitung'][$no_kunci][$no_jawaban][$ngram]	 = $this->matching_kata($hasil[$no_jawaban]['jawaban'], $hasil[$no_kunci]['kunci'], $ngram);
					$hasil['p'][$no_kunci][$no_jawaban][$ngram]				 = $hasil['hasil_hitung'][$no_kunci][$no_jawaban][$ngram]['jumlah'];
					$hasil['jml'][$no_kunci][$no_jawaban][$ngram]			 = $hasil[$no_kunci]['kunci']['jumlah'] - ($ngram-1);
					if($hasil['p'][$no_kunci][$no_jawaban][$ngram]!=0){
						$hasil['p'][$no_kunci][$no_jawaban][$ngram]			 = $hasil['p'][$no_kunci][$no_jawaban][$ngram]/$hasil['jml'][$no_kunci][$no_jawaban][$ngram];
					}else{
						$hasil['p'][$no_kunci][$no_jawaban][$ngram]			 = 0;
					}
					$hasil['log'][$no_kunci][$no_jawaban]					 = $hasil['log'][$no_kunci][$no_jawaban] + log10($hasil['p'][$no_kunci][$no_jawaban][$ngram]);
				}
			}
			$hasil['skor'] = $kalimat['skor'];
		}
		return $hasil;
	}
	
	public function bleu_normal($hasil){
		foreach($hasil['no_kunci'] as $no_kunci){
			foreach($hasil['no'] as $no_jawaban){
				if($hasil['BLEU'][$no_kunci][$no_jawaban]!=0){
					$nilai_BLEU[$no_jawaban][$no_kunci] = $hasil['BLEU'][$no_kunci][$no_jawaban] * $hasil['skor'][$no_kunci];
				}
				else{
					$nilai_BLEU[$no_jawaban][$no_kunci] = 0;
				}
			}
		}
		return $nilai_BLEU;
	}
	//-------BLEU METHOD END-------
	
	
	//TOKENIZING
	function tokenizing($kalimat){
		$hasil 			= "";
		$kalimat 		= explode(" ", trim($kalimat));
		$jml_kalimat 	= count($kalimat);
		$jml_kalimat2 	= $jml_kalimat;
		
		$i = 0;
		while($i<$jml_kalimat){
			$j = $i + 1;
			while($j<$jml_kalimat){
				$hasil_kata_kurang = $this->kata_kurang(strtolower($kalimat[$i]), strtolower($kalimat[$j]));
				//echo$kalimat[$i]."-".$kalimat[$j]." ".$hasil_kata_kurang."<br>";
				if($hasil_kata_kurang==1){
					$k=$j;
					while($k<$jml_kalimat-1){
						$kalimat[$k] = trim($kalimat[$k+1]);
						$k++;
					}
					$jml_kalimat-=1;
				}
				$j++;
			}
			$hasil = trim($hasil)." ".trim($kalimat[$i]);
			$i++;
		}
		//echo$hasil."<br>";
		return $hasil;
	}
	
	//FILTERING
	function ambil_kata_filter(){
		$this->db->select('filter.kata');
		$this->db->from('filter');
		$query = $this->db->get();
		return $query->result_array(); 
	}
	
	function filtering($kalimat){
		$kalimat_2 		= "";
		$kalimat 		= explode(" ", trim($kalimat));
		$jml_kalimat 	= count($kalimat);
		//$filter 		= "antar dalam terdapat yang";
		//$filter 		= explode(" ", trim($filter));
		$filter			= $this->ambil_kata_filter();
		$jml_filter 	= count($filter);
		for($i=0; $i<$jml_filter; $i++){
			$j=0;
			while($j<$jml_kalimat){
				if($filter[$i]==$kalimat[$j]){
					$jml_kalimat-=1;
					for($k=$j; $k<$jml_kalimat; $k++){
						$kalimat[$k] = $kalimat[$k+1];
					}
				}
				else{
					$j++;
				}
			}
		}
		for($k=0; $k<$j; $k++){
			$kalimat_2 = trim($kalimat_2)." ".trim($kalimat[$k]);
		}
		return $kalimat_2;
	}
	
	//SIMBOL
	function simbol($kalimat){	
		$simbol 	= ", . / ; ' [ ] < > ? :  { } ! @ # $ % ^ & * ( ) _ + -";
		$simbol 	= explode(" ", trim($simbol));
		$jml_simbol = count($simbol);
		
		for($i=0; $i<$jml_simbol; $i++){
			$kalimat = str_replace($simbol[$i]," ",$kalimat);
		}
		
		return $kalimat;
	}
	
	//MATCHING
	public function matching_kata($S1, $S2, $ngram){
		$jumlah_1		 = $S1['jumlah'];
		$jumlah_2		 = $S2['jumlah'];
		$match['jumlah'] = 0;
		
		for($i=0;$i<$jumlah_1-($ngram-1);$i++){
			$kata_1 = $this->ngram($S1,$ngram,$i);
			$match['S1'][$kata_1] = $kata_1;
			$match['hasil'][$kata_1] = 0;
			for($j=0;$j<$jumlah_2-($ngram-1);$j++){
				$kata_2 = $this->ngram($S2,$ngram,$j);
				$hasil_kata_kurang = $this->kata_kurang(trim(strtolower($kata_1)),trim(strtolower($kata_2)));
				
				if($hasil_kata_kurang==1){
					$match['hasil'][$kata_1] = $match['hasil'][$kata_1] + 1;
					$match['jumlah']		 = $match['jumlah'] + 1;
				}else{
					$match['hasil'][$kata_1] = $match['hasil'][$kata_1] + 0;
					$match['jumlah']		 = $match['jumlah'] + 0;
				}
				if($match['hasil'][$kata_1]>0){
					$match['S2'][$kata_1] = $kata_1;
				}else{
					$match['S2'][$kata_1] = "-";
				}
			}
		}
		return $match;
	}
	
	//N-GRAM
	function ngram($S1,$ngram, $i){
		$kata_1 = "";
		for($I=$i; $I<$ngram+$i; $I++){
			$kata_1 = trim($kata_1)." ".trim($S1['explode'][$I]);
			$kata_1 = trim(strtolower($kata_1));
		}
		return $kata_1;
	}
	
	
	//PECAH HURUF
	function pecah($input){
	  $output = null;
	  for($h=0; $h<strlen($input); $h++){
		$output[$h]=substr($input,$h,1);
	  }
	  return $output;
	}
	
	//KATA KURANG
	public function kata_kurang($kata_1, $kata_2){
	$kata_cari = $kata_1;
	$kata = $this->sinonim($kata_2);
		
		if($kata){
		$no = 0;
		foreach($kata as $id_kata){
			if($id_kata==$kata_cari){
				$hasil_sementara[$no]=1;
			}else{
				$hasil_sementara[$no]=$this->pencocokan($id_kata,$kata_cari);
			}
			$no++;
		}
		
		$hasil = $hasil_sementara[0];
		for($i=0; $i<$no; $i++){
			if($hasil<$hasil_sementara[$i]){
				$hasil = $hasil_sementara[$i];
			}else{
				$hasil = $hasil;
			}
		}
		}else{
			$id_kata = $kata_2;
			if($id_kata==$kata_cari){
				$hasil=1;
			}else{
				$hasil=$this->pencocokan($id_kata,$kata_cari);
			}
		}
		
		//echo$kata_1." ".$kata_2." ".$hasil."<br>";
		
		return $hasil;
	}
	
	public function pencocokan($kata_1, $kata_2){
		$kata_1_a = $this->pecah(strtolower(trim($kata_1)));
		$kata_2_a = $this->pecah(strtolower(trim($kata_2)));
		$jml_1_a = count($kata_1_a);
		$jml_2_a = count($kata_2_a);
		if($jml_1_a>$jml_2_a){
			$kata_1 = $kata_1_a;
			$kata_2 = $kata_2_a;
			$jml_1 = $jml_1_a;
			$jml_2 = $jml_2_a;
		}else{
			$kata_1 = $kata_2_a;
			$kata_2 = $kata_1_a;
			$jml_1 = $jml_2_a;
			$jml_2 = $jml_1_a;
		}
		$i = 0;
		$j = 0;
		while($i<$jml_1&&$j<$jml_2){
			$point=$this->matching_huruf($kata_1[$i], $kata_2[$j]);
			if($point>0){
				$j++;
			}
			$i++;
		}
		$hasil = $j / $jml_1;
		$batas = $this->toleransi_huruf($jml_1);
		$hasil = $this->cek($hasil, $batas);
		
		return $hasil;
	}
	
	public function matching_huruf($a, $b){
		if($a==$b){$point = 1;}else{$point = 0;}
		return $point;
	}
	
	public function toleransi_huruf($jml_1){
		if($jml_1<=5){
			$batas = ($jml_1-1)/$jml_1;
		}
		else if($jml_1>5&&$jml_1<=10){
			$batas = ($jml_1-2)/$jml_1;
		}
		else{
			$batas = 0.85;
		}
		return $batas;
	}
	
	public function cek($a, $b){
		if($a>=$b){$point = 1;}else{$point = 0;}
		return $point;
	}
	
	//SINONIM
	function ambil_kata_sinonim($kata){
		$this->db->select('sinonim.kata');
		$this->db->from('sinonim');
		$this->db->like('kata', $kata);
		$query = $this->db->get();
		return $query->result_array(); 
	}
	
	function sinonim($kata){
		$kata_explode = explode(' ',$kata);
		$no = 0;
		$hasil[0] = $kata;
		foreach($kata_explode as $kata_id){
			$kata_sinonim = $this->ambil_kata_sinonim($kata_id);
			if($kata_sinonim){
				foreach($kata_sinonim as $sinonim_id){
					$sinonim_explode =explode(',',$sinonim_id['kata']);
					foreach($sinonim_explode as $sinonim){
						$hasil[$no] = str_replace($kata_id,$sinonim,$kata);
						$no++;
					}
				}
			}
		}
		return $hasil;
	}
	
	
	
	/*function sinonim_2($kata1,$kata2){
		$hasil = 0;
		$hasil = $this->kata_kurang(trim(strtolower($kata1)),trim(strtolower($kata2)));
		if($hasil!=1){
			$kata_1 = explode(" ",$kata1);
			foreach($kata_1 as $kata_1_b){
				$kata_1_c = $this->ambil_kata_sinonim($kata_1_b);
				if($kata_1_c){
					foreach($kata_1_c as $kata_1_1){
						$kata_1_1_b = $kata_1_1['kata'];
						$kata_1_1_c = explode(",",$kata_1_1_b);
						foreach($kata_1_1_c as $row){
							$kata_1_1_1 = str_replace($kata_1_b,$row,$kata1);
							$kata_1_1_1.' - '.$kata2.'<br>';
							$hasil_sementara = $this->kata_kurang(trim(strtolower($kata_1_1_1)),trim(strtolower($kata2)));
							if($hasil_sementara>0){
								$hasil = + 1;
							}else{
								$hasil = + 0;
							}
						}
					}
				}
			}
			if($hasil>0){
				$hasil = 1;
			}else{
				$hasil = 0;
			}
		}
		return $hasil;
	}
	
	public function kata_kurang($kata_1, $kata_2){
		$kata_1_a = $this->pecah(strtolower(trim($kata_1)));
		$kata_2_a = $this->pecah(strtolower(trim($kata_2)));
		$jml_1_a = count($kata_1_a);
		$jml_2_a = count($kata_2_a);
		if($jml_1_a>$jml_2_a){
			$kata_1 = $kata_1_a;
			$kata_2 = $kata_2_a;
			$jml_1 = $jml_1_a;
			$jml_2 = $jml_2_a;
		}
		else{
			$kata_1 = $kata_2_a;
			$kata_2 = $kata_1_a;
			$jml_1 = $jml_2_a;
			$jml_2 = $jml_1_a;
		}
		$i = 0;
		$j = 0;
		while($i<$jml_1&&$j<$jml_2){
			$point=$this->matching_huruf($kata_1[$i], $kata_2[$j]);
			if($point>0){
				$j++;
			}
			$i++;
		}
		$hasil = $j / $jml_1;
		$batas = $this->toleransi_huruf($jml_1);
		$hasil = $this->cek($hasil, $batas);
		//if($kata_1==$kata_2){$hasil=1;}else{$hasil=0;}
		return $hasil;
	}
	*/

}